package com.visualexpress.database;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;

public class CreateTables {
	
	private static String getSQL(String path) throws IOException 
	{
		
		StringBuffer cmd = new StringBuffer();
		FileInputStream stream = new FileInputStream(path);
		DataInputStream dis = new DataInputStream(stream);
		String line;
		while((line = dis.readLine())!=null)
		{
			cmd.append(line);
		}
		return cmd.toString();
	}
		
	public static void createTables(String dbName,String user,String pswd,String cmdPath) throws ClassNotFoundException, SQLException
	{
		 String[] cmds = null;
		    try {
		    	 String cmd="";
				cmd = getSQL(cmdPath);
				cmds = cmd.split(";");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:"+dbName, user, 
		pswd);

			Statement stmt = conn.createStatement();
			for(String cmd:cmds)
			{
				stmt.executeUpdate(cmd);
				System.out.println(cmd);
			}
			
			stmt.close();
			conn.close();
	}
	 public static void main(String args[]) {
		 
		 String dbName = "orcl";
		 String user = "globaldb";
		 String pswd = "globaldb";
		 String path = "a.sql";
		 
		 try {
			createTables(dbName,user,pswd,path);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
   
   
}
}
